package sicnu.cs.aps.domain.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.*;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;

/**
 * @author lucker
 * @date 2022年5月26日15:21:32
 */

@With
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "t_activity")
public class Activity implements Serializable {
    private static final long serialVersionUID = -94662526983022948L;
    /**
     * 活动的id
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 活动开始时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(updateStrategy = FieldStrategy.NOT_NULL)
    private Date activityStartTime;

    /**
     * 活动结束时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(updateStrategy = FieldStrategy.NOT_NULL)
    private Date activityEndTime;

    /**
     * 活动内容
     */
    @TableField(updateStrategy = FieldStrategy.NOT_NULL)
    private String content;

    /**
     * 作者
     */
    @TableField(updateStrategy = FieldStrategy.NOT_NULL)
    private Long userId;

    /**
     * 标题
     */
    @TableField(updateStrategy = FieldStrategy.NOT_NULL)
    private String title;

    /**
     * 活动详情创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(updateStrategy = FieldStrategy.NOT_NULL)
    private Date createTime;

    /**
     * 报名截止时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(updateStrategy = FieldStrategy.NOT_NULL)
    private Date signEndTime;

    /**
     * 报名开始时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(updateStrategy = FieldStrategy.NOT_NULL)
    private Date signStartTime;

    /**
     * 附件的uri
     */
    @TableField(updateStrategy = FieldStrategy.NOT_NULL)
    private String uri;

    /**
     * 活动封面URI
     */
    @TableField(updateStrategy = FieldStrategy.NOT_NULL)
    private String cover;

    /**
     * 是否删除
     */
    @TableLogic
    @TableField(updateStrategy = FieldStrategy.NOT_NULL)
    private Integer isDeleted;

    /**
     * 判断各种时间是否合规，如果合规则返回null，否则返回不合规原因
     *
     * @return String
     */
    public String isTimeLegal() {
        if (!this.getActivityStartTime().before(this.getActivityEndTime())) {
            return "活动开始时间必须早于结束时间";
        }
        if (!this.getSignStartTime().before(this.getSignEndTime())) {
            return "报名开始时间必须早于结束时间";
        }
        if (!this.getSignEndTime().before(this.getActivityEndTime())) {
            return "报名结束时间必须早于活动结束时间";
        }
        if (!new Date().before(this.getActivityEndTime())) {
            return "活动结束时间不能晚于当前时间";
        }
        return null;
    }
}
